package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type3_formula4_location_activity.*
import kotlinx.android.synthetic.main.type3_formula5_location_activity.*
import java.lang.NumberFormatException

/**
 *  电子学 - 电声dB的关系
 */
class Type3Formula5Activity : BaseActivity() {

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type3Formula5Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type3_formula5_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula3_5_X1_TEV.reset()
            formula3_5_dB1_TEV.reset()
            formula3_5_X2_TEV.reset()
            formula3_5_dB2_TEV.reset()
            formula3_5_V1_TEV.reset()
            formula3_5_R1_TEV.reset()
            formula3_5_result11_TEV.reset()
            formula3_5_result12_TEV.reset()
            formula3_5_result13_TEV.reset()
            formula3_5_result14_TEV.reset()
            formula3_5_W2_TEV.reset()
            formula3_5_R2_TEV.reset()
            formula3_5_result21_TEV.reset()
            formula3_5_result22_TEV.reset()
            formula3_5_result23_TEV.reset()
            formula3_5_result24_TEV.reset()
            formula3_5_dBu3_TEV.reset()
            formula3_5_R3_TEV.reset()
            formula3_5_V3_TEV.reset()
            formula3_5_W3_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                val result1 = FormulaUtil.formula3_5_1(formula3_5_X1_TEV.getDouble())
                val result2 = FormulaUtil.formula3_5_2(formula3_5_X2_TEV.getDouble())
                val result31 = FormulaUtil.formula3_5_31(formula3_5_V1_TEV.getDouble())
                val result32 = FormulaUtil.formula3_5_32(formula3_5_V1_TEV.getDouble())
                val result33 = FormulaUtil.formula3_5_33(
                    formula3_5_V1_TEV.getDouble(),
                    formula3_5_R1_TEV.getDouble()
                )
                val result34 = FormulaUtil.formula3_5_34(
                    formula3_5_V1_TEV.getDouble(),
                    formula3_5_R1_TEV.getDouble()
                )

                val result41 = FormulaUtil.formula3_5_41(
                    formula3_5_W2_TEV.getDouble(),
                    formula3_5_R2_TEV.getDouble()
                )
                val result42 = FormulaUtil.formula3_5_42(
                    formula3_5_W2_TEV.getDouble(),
                    formula3_5_R2_TEV.getDouble()
                )
                val result43 = FormulaUtil.formula3_5_43(formula3_5_W2_TEV.getDouble())
                val result44 = FormulaUtil.formula3_5_44(formula3_5_W2_TEV.getDouble())

                val result51 = FormulaUtil.formula3_5_51(formula3_5_dBu3_TEV.getDouble())
                val result52 = FormulaUtil.formula3_5_52(
                    formula3_5_dBu3_TEV.getDouble(),
                    formula3_5_R3_TEV.getDouble()
                )

                formula3_5_dB1_TEV.setText(result1)
                formula3_5_dB2_TEV.setText(result2)
                formula3_5_result11_TEV.setText(result31)
                formula3_5_result12_TEV.setText(result32)
                formula3_5_result13_TEV.setText(result33)
                formula3_5_result14_TEV.setText(result34)

                formula3_5_result21_TEV.setText(result41)
                formula3_5_result22_TEV.setText(result42)
                formula3_5_result23_TEV.setText(result43)
                formula3_5_result24_TEV.setText(result44)

                formula3_5_V3_TEV.setText(result51)
                formula3_5_W3_TEV.setText(result52, 4)

            } catch (e: NumberFormatException) {
                formula3_5_dB1_TEV.setText("Infinity")
                formula3_5_dB2_TEV.setText("Infinity")
                formula3_5_result11_TEV.setText("Infinity")
                formula3_5_result12_TEV.setText("Infinity")
                formula3_5_result13_TEV.setText("Infinity")
                formula3_5_result14_TEV.setText("Infinity")

                formula3_5_result21_TEV.setText("Infinity")
                formula3_5_result22_TEV.setText("Infinity")
                formula3_5_result23_TEV.setText("Infinity")
                formula3_5_result24_TEV.setText("Infinity")

                formula3_5_V3_TEV.setText("Infinity")
                formula3_5_W3_TEV.setText("Infinity")
            }
        }
    }
}